const models = require("../models/index.js");
const Client = models.Client;
const uuid = require("uuid");
const bcrypt = require("bcrypt");
const mailService = require("./mail-service.js");

class ManadgerService {
  // ---- регистрация клиента -------------
  async registrationClient(manadgerId, email, password) {
    const candidate = await Client.findOne({ where: { email } });
    if (candidate) {
      throw new Error(`Пользователь с почтой ${email} уже существует`);
    }
    const hashPassword = await bcrypt.hash(password, 5);
    const activationLink = uuid.v4();
    const client = await Client.create({
      email,
      password: hashPassword,
      activationLink,
      manadgerId: manadgerId,
    });
    await mailService.sendActivationMail(
      email,
      `${process.env.API_URL}/api/client/activate/${activationLink}`
    );

    return { client };
  }
}
module.exports = new ManadgerService();
